package com.neuedu.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.neuedu.model.Notice;
import com.neuedu.service.NoticeService;
import com.neuedu.util.StringUtil;

@Controller
@RequestMapping(value = "/admin/Notice")
public class NoticeAction {
	@Autowired
	private NoticeService noticeService;

	// 查询所有
	@RequestMapping(value = "/findAll")
	public void findAll(Notice notice,HttpServletRequest request, HttpServletResponse response) throws Exception {

		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));

		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> pageMap = new HashMap<String, Object>();
		pageMap.put("startPage", (page - 1) * rows); // 从第几条数据开始查询
		pageMap.put("endPage", rows); // 每页查询多少条记录
		pageMap.put("noticeTitle", StringUtil.formatString(notice.getNoticeTitle()));
		
		List<Notice> c = noticeService.findAll(pageMap);
		int total = noticeService.countNotice();

		map.put("rows", c);
		map.put("total", total);

		String str = JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}

	// 添加
	@RequestMapping(value = "addNotice")
	public void addNotice(Notice notice, HttpServletRequest request, HttpServletResponse response) throws Exception {
		int res = noticeService.addNotice(notice);
		System.out.println(res);
		Map<String, Object> map = new HashMap<String, Object>();
		if (res > 0) {
			map.put("success", true);
		} else {
			map.put("success", false);
			map.put("errorMsg", "Save user fail !!!");
		}
		String str = JSONObject.toJSONString(map);
		response.getWriter().write(str);

	}

	// 删除
	@RequestMapping(value = "deleteNotice")
	public void deleteNotice(Integer noticeNo, HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		int res = noticeService.deleteNotice(noticeNo);
		Map<String, Object> map = new HashMap<String, Object>();

		if (res > 0) {
			map.put("success", true);
		} else {
			map.put("success", false);
			map.put("errorMsg", "update user fail !!!");
		}

		String str = JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}
}
